<?php
class yx_dataModel extends model
{
	public function __construct(){
		header("Content-Type: text/html; charset=utf-8");
		set_time_limit(0);//设置不超时.		
		ini_set('memory_limit', '10240M');
		parent::__construct();

		$this->data = $this->loadModel('data');
		$this->p =  $this->app->loadClass('proving');//加载相关类
		
		$ym = date('Ym',time()-24*3600);
		$ym = '202007';
		$this->ordercustomer = 'ordercustomer_'.$ym;
		$this->orderdetail = 'orderdetails_'.$ym;
	}
	
	public function get_cs_cus(){
		$sql = "select * from t_comm_business where yx_user_id is null or yx_user_id = '' and operating = '2701'";
		return $this->dao->execute($sql);
	}
	
	public function get_ybcus($startdate,$enddate){
		return $this->dao->select('c.license,b.id as sample_record_id,c.id as customer_id')->from('col_record')->alias('a')
						->leftJoin('col_record_sample')->alias('b')->on('a.id = b.record_id')
						->leftJoin('basic_customer')->alias('c')->on('b.sample_id = c.id')
						//->where("task_date between '$startdate' and '$enddate' and b.status = 1 and a.status = 1")
						->fetchAll();
	}
	
	public function get_col_cigarette(){
		return $this->dao->select('*')->from('col_cigarette')->where('isupdata = 1 and status = 1')->fetchAll();
	}
	
	//更新样本订单数据
	public function getyborders($data){
		
		$data[7]->value = preg_replace('|[0-9a-zA-Z/]+|', '', $data[7]->value);
		$data[12]->value = preg_replace('|[0-9a-zA-Z/]+|', '', $data[12]->value);
		
		$wl_user_name = str_replace(array(' ','\t'),'',$data[7]->value);
		$yx_user_name = str_replace(array(' ','\t'),'',$data[12]->value);
		
		$wl = $this->dao->select('*')->from('t_comm_user')->where('employee_name')->eq($wl_user_name)->fetch();
		$yx = $this->dao->select('*')->from('t_comm_user')->where('employee_name')->eq($yx_user_name)->fetch();

		$wl_user_id = '';
		$wl_tel = '';
		$yx_user_id = '';
		$yx_tel = '';
		
		if($wl){
			$wl_user_id = $wl->id;
			$wl_tel = $wl->mobile_phone;
		}else{
			$uuid = $this->dao->uuid();
			$this->dao->insert('t_comm_no_user')		
			->set('id')->eq($uuid)		
			->set('wl_user_name')->eq($wl_user_name)		
			->set('license')->eq($data[4]->value)			
			->set('adddtime')->eq(time())
			->exec();
		}
		
		if($yx){
			$yx_user_id = $yx->id;
			$yx_tel = $yx->mobile_phone;
		}else{
			$uuid = $this->dao->uuid();
			$this->dao->insert('t_comm_no_user')		
			->set('id')->eq($uuid)		
			->set('yx_user_name')->eq($yx_user_name)		
			->set('license')->eq($data[4]->value)			
			->set('adddtime')->eq(time())
			->exec();
		}
		$is_exits = $this->dao->select('id')->from('t_comm_business')->where('license')->eq($data[4]->value)->fetch();
		if(!$is_exits){
			$uuid = $this->dao->uuid();
			$this->dao->insert('t_comm_business')		
		->set('id')->eq($uuid)		
		->set('code')->eq($data[1]->value)		
		->set('legal')->eq($data[2]->value)		
		->set('name')->eq($data[3]->value)		
		->set('license')->eq($data[4]->value)
		->set('helpcode')->eq($data[5]->value)
		//->set('step')->eq($data[6]->value)		
		->set('deliveryman')->eq($data[7]->value)		
		->set('route')->eq($data[8]->value)		
		->set('address')->eq($data[9]->value)		
		//->set('company_name')->eq($data[10]->value)		
		->set('yx_marketDept')->eq($data[11]->value)		
		//->set('manager_name')->eq($data[12]->value)		
		->set('tel')->eq($data[13]->value)
		->set('ordertel')->eq($data[14]->value)			
		->set('deliverytel')->eq($data[15]->value)			
		->set('remark_tel1')->eq($data[16]->value)			
		->set('remark_tel2')->eq($data[17]->value)			
		->set('orderingcycle')->eq($data[18]->value)		
		->set('orderform')->eq($data[19]->value)		
		->set('customer_cat')->eq($data[20]->value)		
		->set('receipt_type')->eq($data[21]->value)		
		->set('tradingarea')->eq($data[22]->value)		
		->set('terminal_type')->eq($data[23]->value)
		->set('wl_user_id')->eq($wl_user_id)
		->set('wl_tel')->eq($wl_tel)
		->set('yx_user_name')->eq($yx_user_name)
		->set('yx_user_id')->eq($yx_user_id)
		->set('yx_tel')->eq($yx_tel)
					->exec();
		}else{
			
			$this->dao->update('t_comm_business')		
					->set('code')->eq($data[1]->value)		
					->set('legal')->eq($data[2]->value)		
					->set('name')->eq($data[3]->value)		
					->set('license')->eq($data[4]->value)
					->set('helpcode')->eq($data[5]->value)
					//->set('step')->eq($data[6]->value)		
					->set('deliveryman')->eq($data[7]->value)		
					->set('route')->eq($data[8]->value)		
					->set('address')->eq($data[9]->value)		
					//->set('company_name')->eq($data[10]->value)		
					->set('yx_marketDept')->eq($data[11]->value)		
					//->set('manager_name')->eq($data[12]->value)		
					->set('tel')->eq($data[13]->value)
					->set('ordertel')->eq($data[14]->value)			
					->set('deliverytel')->eq($data[15]->value)			
					->set('remark_tel1')->eq($data[16]->value)			
					->set('remark_tel2')->eq($data[17]->value)			
					->set('orderingcycle')->eq($data[18]->value)		
					->set('orderform')->eq($data[19]->value)		
					->set('customer_cat')->eq($data[20]->value)		
					->set('receipt_type')->eq($data[21]->value)		
					->set('tradingarea')->eq($data[22]->value)		
					->set('terminal_type')->eq($data[23]->value)
					/* ->set('wl_user_id')->eq($wl_user_id)
					->set('wl_tel')->eq($wl_tel)
					->set('yx_user_name')->eq($yx_user_name)
					->set('yx_user_id')->eq($yx_user_id)
					->set('yx_tel')->eq($yx_tel) */
					->where('license')->eq($data[1]->value)
					->exec();
		}
		
		return true;
	}
	
	//添加订单
	public function insert_order($data,$list,$p){
		$mid = $this->dao->uuid();
		$customer = $data['customer'];
		$order = $data['order'];
		
		if($customer['营销中心：'] == '城区分公司'){
			$customer['营销中心：'] = '直属分公司';
		}
		
		/* $code_new = $this->dao->select('code_new')->from('d_manager')
				->where('name')->eq($customer['客户经理：'])
				->andWhere('company')->eq($customer['营销中心：'])
				->fetch(); */
		
		//判断订单号存在删除重新添加
		$order_c = $this->dao->select('*')->from($this->ordercustomer)
		        ->where('ordernum')->eq($customer['订单编号：'])
		        ->fetchAll();
		 
		 if($order_c){
		     foreach($order_c as $v){
		         $this->dao->delete()->from($this->orderdetail)->where('mainid')->eq($v->id)->exec();
		         $this->dao->delete()->from($this->ordercustomer)->where('id')->eq($v->id)->exec();
		     }
		 }
		$date = $list['date'];
		$order_number = $list['order_number'];
		$order_amount = $list['order_amount'];
		
		$this->dao->insert($this->ordercustomer)
			->set('id')->eq($mid)
			->set('license')->eq($customer['客户编码：'])
			->set('customer')->eq($customer['客户名称：'])
			->set('ordernum')->eq($customer['订单编号：'])
			->set('company')->eq($customer['营销中心：'])
			->set('department')->eq($customer['营销部：'])
			->set('manager')->eq($customer['客户经理：'])
			->set('orderstatus')->eq($customer['订单状态：'])
			->set('paystat')->eq($customer['付款状态：'])
			->set('settlementway')->eq($customer['结算方式：'])
			->set('businessstatus')->eq($customer['经营业态：'])
			->set('position')->eq($customer['地理位置：'])
			->set('size')->eq($customer['经营规模：'])
			->set('validity')->eq($customer['访销周期：'])
			->set('ordertel')->eq($customer['订货电话：'])
			//->set('code_new')->eq(@$code_new->code_new)
			->set('orderdate')->eq(date('Y-m-d',strtotime($date)))
			->set('ordermonth')->eq(date('m',strtotime($date)))
			->set('orderyear')->eq(date('Y',strtotime($date)))
			->set('ordertime')->eq(strtotime($date))
			->set('order_number')->eq($order_number)
			->set('order_amount')->eq($order_amount)
			->set('page')->eq($p)
			->set('addtime')->eq(time())
			->exec();
		
		foreach($order as $v){
			$did = $this->dao->uuid();
			$this->dao->insert($this->orderdetail)
				->set('id')->eq($did)
				->set('mainid')->eq($mid)
				->set('shopnum')->eq($v[1])
				->set('shopname')->eq($v[2])
				->set('unitprice')->eq($v[3])
				->set('neednum')->eq($v[4])
				->set('quantitative')->eq($v[5])
				->set('ordernumber')->eq($v[6])
				->set('amount')->eq($v[7])
				->set('remark')->eq($v[8])
				->set('addtime')->eq(time())
				->exec();
		}

	}
	
	public function update_order_log($total,$p,$date=''){
		$id = $this->dao->uuid();
		
		$page_num = $this->dao->select('count(id) as n')->from($this->ordercustomer)
				->where('page')->eq($p)
				->andWhere('orderdate')->eq(date('Y-m-d',strtotime($date)))
				->fetch();
				
		$cur_num = $this->dao->select('count(id) as n')->from($this->ordercustomer)
				->where('orderdate')->eq(date('Y-m-d',strtotime($date)))
				->fetch();
		
		$log_exeot = $this->dao->select('*')->from('dog_log_order')
				->where('page')->eq($p)
				->andWhere('status')->eq(1)
				->fetch();
	
		if($log_exeot){
			$this->dao->update('dog_log_order')
				->set('status = 0')
				->where('page')->eq($p)
				->andWhere('status')->eq(1)
				->exec();
			
		}else{
			/* $did = $this->dao->uuid();
			$this->dao->insert('dog_log_order')
					->set('id')->eq($did)
					->set('total')->eq($total['total'])
					->set('total_page')->eq($total['page'])
					->set('page')->eq($p)
					->set('page_num')->eq($page_num->n)
					->set('cur_num')->eq($cur_num->n)
					->set('addtime')->eq(time())
					->exec(); */
		}
		
	}
	
	
	//更新商户信息
	public function updatecustomerinfo($data){
		$this->dao->update('t_comm_business_grab')
				->set('faith_level')->eq(@$data[7]->value)
				->where('license')->eq($data[4]->value)
				->exec();
		
		return true;
	}
	
	//获取订单列表数据
	public function order_list1($data){
		$r = array();
		preg_match_all('/<tr class="dno".*>(.*)<\/tr>/isU',$data,$array);
		
		foreach($array[1] as $k => $v){
			preg_match_all('/<td.*>(.*)<\/td>/isU',$v,$td);
			
			preg_match_all('/.*>(.*)<.*/isU',$td[1][0],$a);
			
			$r[$k]['number'] = $a[1][0];
			//$r[$k]['date'] = $td[1][2];
			$r[$k]['date'] = $td[1][4];
			$r[$k]['order_number'] = $td[1][9];
			$r[$k]['order_amount'] = $td[1][10];
		}
		
		return $r;
	}
	
	//获取订单详情数据
	public function order_details($data){
		ini_set('pcre.backtrack_limit', 999999999);
		
		$return = array();
		preg_match_all('/<table.*>(.+)<\/table>/isU',$data,$array);
		$customer = $array[1][0];
		$orderinfo = $array[1][1];
		
		preg_match_all('/<tr.*>(.+)<\/tr>/isU',$customer,$c_tr);
		preg_match_all('/<tr.*>(.+)<\/tr>/isU',$orderinfo,$o_tr);
		
		//获取客户信息
		$cus = array();
		foreach($c_tr[1] as $cv){
			preg_match_all('/<td.*>(.+)<\/td>/isU',$cv,$c);
			foreach($c[1] as $v){
				$cus[] = trim(strip_tags($v));
			}
		}
		
		$customerinfo = array();
		foreach($cus as $k => $v){
			if($k%2 == 0){
				$rk = @$cus[$k];
				$rk1 = @$cus[$k+1];
				$customerinfo[$rk] = $rk1;
			}
		}
		
		//截取订单信息
		$order = array();
		foreach($o_tr[1] as $k => $ov){
			preg_match_all('/<td.*>(.+)<\/td>/isU',$ov,$o);
			foreach($o[1] as $v){
				$order[$k][] = trim(strip_tags($v));
			}
		}
		
		$return['customer'] = $customerinfo;
		$return['order'] = $order;
		
		return $return;
	}
	
	public function total_page_num($data){
		//a($data);exit;
		preg_match_all('/<div class="pageBarContainer">(.*)<div class="right">/isU',$data,$p_heml);

		$return = array();
		preg_match_all('/共(.*)条/isU',$p_heml[1][0],$total);
		preg_match_all('/ 分(.*)页/isU',$p_heml[1][0],$page);
		
		$return['total'] = $total[1][0];
		$return['page'] = $page[1][0];
		
		return $return;
	}
	
	//获取订单列表数据
	public function order_list2($data){
		$r = array();
		preg_match_all('/<tr class="dno".*>(.+)<\/tr>/isU',$data,$array);
		
		foreach($array[1] as $k => $v){
			preg_match_all('/<td.*>(.+)<\/td>/isU',$v,$td);
			//a($array);exit;
			preg_match_all('/<a.*>(.+)<\/a>/isU',$td[1][0],$a);

			$r['ordernum'] = $td[1][15];
			
		}
		
		return $r;
	}

	//页面追加内容
	public function push_div_content($div,$content){
		echo('<script>
			var h = $(".scorpall")[0].offsetHeight;
			$("body").scrollTop(h); 
			$("'.$div.'").append("'.$content.'")
		</script>');
		ob_flush();
		flush();
	}


}
